/*******************************************************************************
 * 版权：北京润尼尔网络科技有限公司，保留所有版权<br>
 * 版本：v1.0.0<br>
 * 文件：ConnectionEditPart.js<br>
 * 日期：2016年9月20日<br>
 * 作者:王志远<br>
 * 功能：连接编辑件，作为MVC的Controller，传递Model和Figure操作<br>
 * 
 * 修改记录：<br>
 * 日期 描述 更新功能<br>
 ******************************************************************************/

window.makeNamespace(window,"Rainier.UI.V2");

/**
 * 类型名称：Rainier.UI.V2.ConnectionEditPart<br>
 * 类型功能：组合组件编辑件<br>
 */
Rainier.UI.V2.ConnectionEditPart = Rainier.UI.V2.CompositeEditPart.extend({
	
	NAME : "Rainier.UI.V2.ConnectionEditPart",
	
	/**
	 * 函数名称：Rainier.UI.V2.ConnectionEditPart.init<br>
	 * 函数功能：构造函数<br>
	 * 异常信息：无<br>
	 */
	init : function(){
		this._super();
	},

	/**
	 * 函数名称：Rainier.UI.V2.ConnectionEditPart.createFigure<br>
	 * 函数功能：创建连接图元<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	createFigure : function(){
		var model 		 = this.model;
		var uiProperty   = this.model.property.uiProperty;
		var modelProperty= this.model.property.modelProperty;
		var router       = modelProperty.router;
		var segments     = modelProperty.segments;
		
		var _segments    = new draw2d.util.ArrayList();
		var vertices	 = new draw2d.util.ArrayList();
		
		for(var i=0;i<segments.length;i++){
			 var seg  = segments[i];
			 var _seg = {};
			 _seg.start = new draw2d.geo.Point(seg.start_x,seg.start_y);
			 _seg.end   = new draw2d.geo.Point(seg.end_x,seg.end_y);
			 _seg.type  = seg.type;
			 _segments.add(_seg);
		}
		
		_segments.each(function(i,seg){
			vertices.add(seg.start);
		});
		vertices.add(_segments.last().end);
		
		var connFigure = new Rainier.UI.V2.Connection(uiProperty);
		connFigure.setRouter(eval("new " + router + "();"));
		connFigure.setVertices(vertices);
		
		//设置器件Label
		connFigure.setNodeLabelFigure(
			{
				text 		: '',
				fontSize	: 10,
				bold		: true,
				fontColor	:'#ff0000'
			}
		);
		
		this.figure = connFigure;		
		this._super();
		return connFigure;
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.AbstractEditPart.onFigureChange<br>
	 * 函数功能：当图元属性变化时触发此函数<br>
	 * 参数输入：emitter<br>
	 * 参数输入：figure<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	onFigureChange : function(emitter, figure){
		var vertices = this.figure.getVertices();
		var segments = this.convertVerticesToSegments(vertices);
		this.model.setUiProperty("vertices",vertices,false,this);
		this.model.getProperty().modelProperty.segments = segments;
		
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.AbstractEditPart.convertVerticesToSegments<br>
	 * 函数功能：转vertices为segments<br>
	 * 参数输入：vertices ：线段列表<br>
	 * 参数输出：无<br>
	 * 函数返回：segments ：线段对象列表<br>
	 * 异常信息：无<br>
	 */
	convertVerticesToSegments : function(vertices){
		var segments = []; 
		for(var i = 0;i<vertices.getSize() - 1;i++){
			var segment = {};
			segment.start_x = vertices.get(i).x;
			segment.start_y = vertices.get(i).y;
			segment.end_x = vertices.get(i+1).x;
			segment.end_y = vertices.get(i+1).y;
    	   if(segment.start_x === segment.end_x){
    		   segment.type  = "vertical";
    	   }
    	   if(segment.start_y === segment.end_y){
    		   segment.type  = "horizontal";
    	   }
    	   segments.push(segment);
		}
		return segments;
	},
		
	/**
	 * 函数名称：Rainier.UI.V2.ConnectionEditPart.onRequestCommand<br>
	 * 函数功能：当请求命令时发出<br>
	 * 参数输入：request<br>
	 * 函数返回：draw2d.command.Command<br>
	 * 异常信息：<br>
	 */
	onRequestCommand : function(request){
		
	    if(request.getPolicy() === draw2d.command.CommandType.MOVE){
	    	if(this.figure.isDraggable()){
	    		return new Rainier.UI.V2.CommandMoveVertices(this);
		    }
		}
		
		if(request.getPolicy() === draw2d.command.CommandType.MOVE_VERTICES){
			if(this.figure.isDraggable()){
				//不在model层进行处理，connectionModel.modelProperty.dummy记录
				//了连接图元实例，关于这些变化完全可以忽略
				return new Rainier.UI.V2.CommandMoveVertices(this);
			}
		}

		if(request.getPolicy() === draw2d.command.CommandType.DELETE){
			if(this.figure.isDeleteable()){
				return new Rainier.UI.V2.CommandDelete(this);
			}
		}

		if(request.getPolicy() === draw2d.command.CommandType.MOVE_VERTEX){
			if(this.figure.isDraggable()){
				//不在model层进行处理，connectionModel.modelProperty.dummy记录
				//了连接图元实例，关于这些变化完全可以忽略
				return new Rainier.UI.V2.CommandMoveVertex(this);
			}
		}
		
	    return this._super(request);
	},

	/**
	 * 函数名称：Rainier.UI.V2.AbstractEditPart.handleModelEvent<br>
	 * 函数功能：处理模型事件<br>
	 * 参数输入：modelEvent:模型事件<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	handleModelEvent : function(modelEvent){
		
		var eventName = modelEvent.getEventName();
		var newValue  = modelEvent.getNewValue();
		var figure    = this.getFigure();
		var modelPro  = this.getModel().property.modelProperty;

		switch(eventName){
			//ConnectionModel事件定义：显示结点标签
			case Rainier.UI.V2.ConnectionModel.MODEL_EVENT_SHOW_NODE_LABEL :
				figure.showNodeLabel();
				break;
			//ConnectionModel事件定义：隐藏节点标签
			case Rainier.UI.V2.ConnectionModel.MODEL_EVENT_HIDE_NODE_LABEL :
				figure.unshowNodeLabel();
				break;
			//ConnectionModel事件定义：修改节点标签
			case Rainier.UI.V2.ConnectionModel.MODEL_EVENT_CHANGE_NODE_LABEL :
				figure.changeNodeLabel(newValue.nodeLabel);
				break;
		}
		this._super(modelEvent);
	},
	
	/**
	 * 函数名称：Rainier.UI.V2.ConnectionEditPart.onFigureContextMenu<br>
	 * 函数功能：当图元右键菜单时触发<br>
	 * 参数输入：emitter<br>
	 * 参数输入：figure<br>
	 * 函数返回：无<br>
	 * 异常信息：无<br>
	 */
	onFigureContextMenu : function(emitter, figure){
		//nothing todo
//		if(emitter instanceof draw2d.Connection){
//			var connectionMenu = new Rainier.UI.V2.ConnectionMenu(emitter,figure);
//			connectionMenu.show();
//		}
	},
});

